﻿Imports System.IO
Imports System.Xml
Module LoadHelp
    Private m_xmld As New XmlDocument
    Private HelpFileLoaded As Boolean
    Public Function ShowHelp(ByVal Initial As String, ByVal Password As String, Optional ByRef DoNumberInstead As Boolean = False)
        If HelpFileLoaded = False Then 'make sure we don't load the file every time we ask a help file
            Try
                m_xmld.Load(Replace(Replace(MainProgram.DirHelp, ".TG", ".xml"), "\\", "\"))
                HelpFileLoaded = True
            Catch errorvariable As Exception 'just incase file is corrupt or missing
                MainProgram.SND("/RERROR: " + errorvariable.ToString())
            End Try
        End If
        'declare xml dimensions
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlNode
        Dim CurNumber, MaxNumber As Integer
        Dim CurInitial As String 'current help file
        Dim CurType As String 'type of help display
        Try
            m_nodelist = m_xmld.SelectNodes("/helplist/help")
            For Each m_node In m_nodelist 'for each help in helplist
                If DoNumberInstead = True Then 'search help by number or name
                    CurInitial = m_node.Attributes.GetNamedItem("number").Value
                Else
                    CurInitial = m_node.Attributes.GetNamedItem("file").Value
                End If
                If CurInitial = Initial Then
                    CurType = m_node.ChildNodes.Item(1).Attributes.GetNamedItem("type").Value
                    If CurType = "lines" Then
                        'show the current help file number and total help files
                        MainProgram.SND("/Z[/YHelp file/Z: /y" + m_node.Attributes.GetNamedItem("number").Value + "/Z//y" + LTrim(Str(m_nodelist.Count)) + "/Z]")
                        MaxNumber = Val(m_node.ChildNodes.Item(1).Attributes.GetNamedItem("number").Value) - 1
                        If MaxNumber > 0 Then
                            For CurNumber = 0 To MaxNumber Step 1
                                MainProgram.SND(m_node.ChildNodes.Item(1).ChildNodes.Item(CurNumber).InnerText)
                            Next
                        Else
                            MainProgram.SND("/RERROR : Help file lines type has no lines /Y@/Whelp#/Z" + m_node.Attributes.GetNamedItem("number").Value)
                        End If
                    ElseIf CurType = "single" Then
                        'show the current help file number and total help files
                        MainProgram.SND("/Z[/YHelp file/Z: /y" + m_node.Attributes.GetNamedItem("number").Value + "/Z//y" + LTrim(Str(m_nodelist.Count)) + "/Z]")
                        'show the proper usage / command names under node "type"
                        MainProgram.SND(m_node.ChildNodes.Item(0).ChildNodes.Item(0).InnerText)
                        'show the data node
                        MainProgram.SND(m_node.ChildNodes.Item(1).ChildNodes.Item(0).InnerText)
                    End If
                    Exit For 'found what we're looking for, quit recursion and return to processes
                End If
            Next
            Return 1
        Catch errorVariable As Exception
            MainProgram.SND("/RERROR: " + errorVariable.ToString())
            Return 0
        End Try
    End Function
End Module